#pragma once
#include <memory>
#include <mysql/mysql.h>
#include <iostream>
#include <string>

#include "LockGuard.hpp"
#include "log.hpp"

const std::string _host;//"localhost"
const std::string _user;// = "connector";
const std::string _password;// = "5397687p";
const std::string _db;// = "conn";
const unsigned int _port;// = 3306; // 确认你的MySQ
#include <iostream>
#include <mysql/mysql.h>
#include <string>

class Database
{
public:
    Database(const std::string &host, const std::string &user, const std::string &password, const std::string &db, const unsigned int port)
        : _host(host), _user(user), _password(password), _db(db), _port(port), my(nullptr)
    {
    }

    ~Database()
    {
        if (my)
        {
            mysql_close(my);
        }
    }

    bool ConnectToSql()
    {
        my = mysql_init(nullptr);
        if (my == nullptr)
        {
            std::cout << "MySQL initialization failed" << std::endl;
            return false;
        }

        if (mysql_real_connect(my, _host.c_str(), _user.c_str(), _password.c_str(), _db.c_str(), _port, nullptr, 0) == nullptr)
        {
            std::cout << "Connection error: " << mysql_error(my) << std::endl;
            mysql_close(my);
            my = nullptr;
            return false;
        }

        return true;
    }

    bool Login(const std::string &name, const std::string &password)
    {
        std::string query = "SELECT * FROM users WHERE name = '" + name + "' AND password = '" + password + "'";
        if (mysql_query(my, query.c_str()))
        {
            std::cout << "Login query failed: " << mysql_error(my) << std::endl;
            return false;
        }

        MYSQL_RES *res = mysql_store_result(my);
        if (res == nullptr)
        {
            std::cout << "Login store result failed: " << mysql_error(my) << std::endl;
            return false;
        }

        int num_rows = mysql_num_rows(res);
        mysql_free_result(res);

        return num_rows > 0;
    }

    bool UserExists(const std::string &name)
    {
        std::string query = "SELECT * FROM users WHERE name = '" + name + "'";
        if (mysql_query(my, query.c_str()))
        {
            std::cout << "UserExists query failed: " << mysql_error(my) << std::endl;
            return false;
        }

        MYSQL_RES *res = mysql_store_result(my);
        if (res == nullptr)
        {
            std::cout << "UserExists store result failed: " << mysql_error(my) << std::endl;
            return false;
        }

        int num_rows = mysql_num_rows(res);
        mysql_free_result(res);

        return num_rows > 0;
    }

    bool Register(const std::string &name, const std::string &password)
    {
        if (UserExists(name))
        {
            std::cout << "User already exists" << std::endl;
            return false;
        }

        std::string query = "INSERT INTO users (name, password) VALUES ('" + name + "', '" + password + "')";
        if (mysql_query(my, query.c_str()))
        {
            std::cout << "Register query failed: " << mysql_error(my) << std::endl;
            return false;
        }

        return true;
    }

private:
    const std::string _host;
    const std::string _user;
    const std::string _password;
    const std::string _db;
    const unsigned int _port;
    MYSQL *my;

pthread_mutex_t _mutex;

};


